﻿/*
Typps HTML Editor for ASP.NET
FULL EDITION

Copyright (c) 2009 Alessandro Zifiglio, http://www.Typps.com
EMail: alessandro@Typps.com

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
Typps.ModalPopup=function () {
    Typps.ModalPopup.initializeBase(this);
    this._id=null;
    this._display=null;
    this._content=null;
    this._isModal=null;
    this._className=null;
    this._shaderColor=null;
    this._shaderOpacity=20;
    this._makeFormChild=null;
    this._okButtonId=null;
    this._cancelButtonId=null;
    this._closeDelegate=null;
    this._cancelButtonDelegate=null;
    this._okButtonDelegate=null;
    this._mouseDownDelegate=null;
    this._targetControlId=null;
    this._targetControlDelegate=null;
}
Typps.ModalPopup.prototype={
    get_id: function () {
        return this._id;
    },
    set_id: function (value) {
        this._id=value;
    },
    get_targetControlId: function () {
        return this._targetControlId;
    },
    set_targetControlId: function (value) {
        this._targetControlId=value;
    },
    get_display: function () {
        return (this._display==null)?false:this._display;
    },
    set_display: function (value) {
        this._display=value;
    },
    get_content: function () {
        return this._content;
    },
    set_content: function (value) {
        this._content=value;
    },
    get_title: function () {
        return this._title;
    },
    set_title: function (value) {
        this._title=value;
    },
    get_isModal: function () {
        return this._isModal==null?true:this._isModal;
    },
    set_isModal: function (value) {
        this._isModal=value;
    },
    get_className: function () {
        return this._className;
    },
    set_className: function (value) {
        this._className=value;
    },
    get_shaderColor: function () {
        return this._shaderColor;
    },
    set_shaderColor: function (value) {
        this._shaderColor=value;
    },
    get_shaderOpacity: function () {
        return this._shaderOpacity;
    },
    set_shaderOpacity: function (value) {
        this._shaderOpacity=value;
    },
    get_makeFormChild: function () {
        return (this._makeFormChild==null)?false:this._makeFormChild;
    },
    set_makeFormChild: function (value) {
        this._makeFormChild=value;
    },
    get_okButtonId: function () {
        return this._okButtonId;
    },
    set_okButtonId: function (value) {
        this._okButtonId=value;
    },
    get_cancelButtonId: function () {
        return this._cancelButtonId;
    },
    set_cancelButtonId: function (value) {
        this._cancelButtonId=value;
    },
    get_formElementId: function () {
        return this._formElementId;
    },
    set_formElementId: function (value) {
        this._formElementId=value;
    },
    add_targetControl: function (handler) {
        this.get_events().addHandler('target',handler);
    },
    remove_targetControl: function (handler) {
        this.get_events().removeHandler('target',handler);
    },
    add_popupCloseButton: function (handler) {
        this.get_events().addHandler('close',handler);
    },
    remove_popupCloseButton: function (handler) {
        this.get_events().removeHandler('close',handler);
    },
    add_cancelButton: function (handler) {
        this.get_events().addHandler('cancel',handler);
    },
    remove_cancelButton: function (handler) {
        this.get_events().removeHandler('cancel',handler);
    },
    add_okButton: function (handler) {
        this.get_events().addHandler('ok',handler);
    },
    remove_okButton: function (handler) {
        this.get_events().removeHandler('ok',handler);
    },
    // Release resources before control is disposed.
    dispose: function () {
        var caption=$get(this._id+'_caption');
        var popupCloseButton=$get(this._id+'_popupCloseButton');

        var targetControl=null;
        if(this._targetControlId!=null)
            targetControl=$get(this._targetControlId);

        if(this._closeDelegate) {
            if(popupCloseButton!=null) {
                Sys.UI.DomEvent.removeHandler(popupCloseButton,'click',this._closeDelegate);
                Sys.UI.DomEvent.removeHandler(popupCloseButton,'mousedown',this._mouseDownDelegate);
            }
            delete this._closeDelegate;
        }
        if(this._startDragDelegate) {
            if(caption!=null)
                Sys.UI.DomEvent.removeHandler(caption,'mousedown',this._startDragDelegate);
            delete this._startDragDelegate;
        }
        if(this._okButtonDelegate) {
            var ok=$get(this._okButtonId);
            if(ok!=null) {
                Sys.UI.DomEvent.removeHandler(ok,'click',this._okButtonDelegate);
                Sys.UI.DomEvent.removeHandler(ok,'mousedown',this._mouseDownDelegate);
            }
            delete this._okButtonDelegate;
        }
        if(this._cancelButtonDelegate) {
            var cancel=$get(this._cancelButtonId);
            if(cancel!=null) {
                Sys.UI.DomEvent.removeHandler(cancel,'click',this._cancelButtonDelegate);
                Sys.UI.DomEvent.removeHandler(cancel,'mousedown',this._mouseDownDelegate);
            }
            delete this._cancelButtonDelegate;
        }
        if(this._targetControlDelegate) {
            Sys.UI.DomEvent.removeHandler(targetControl,'click',this._targetControlDelegate);
        }
        delete this._targetControlDelegate;

        if(this._onResizeHandlerDelegate) {
            Sys.UI.DomEvent.removeHandler(window,'resize',this._onResizeHandlerDelegate);
            delete this._onResizeHandlerDelegate;
        }
        if(this._onScrollHandlerDelegate) {
            Sys.UI.DomEvent.removeHandler(window,'scroll',this._onScrollHandlerDelegate);
            delete this._onScrollHandlerDelegate;
        }
        delete this._mouseDownDelegate;
        var shadeElement=$get(this.get_id()+'_dialogframe');
        if(shadeElement!=null)
            shadeElement.parentNode.removeChild(shadeElement);

        Sys.Application.removeComponent(this);
        if(this._popup!=null) {
            this._popup.parentNode.removeChild(this._popup);
            delete this._popup;
        }
        Typps.ModalPopup.callBaseMethod(this,'dispose');
    },
    initialize: function () {
        Typps.ModalPopup.callBaseMethod(this,'initialize');
        this._popup=null;
        this._popupHeader=null;
        this._selects=null;
        this._startDragDelegate=null;
        this._moveDelegate=null;
        this._stopDragDelegate=null;
        this._cursorX=null;
        this._cursorY=null;
        this._element=null;
        
        this._form = Typps.Utility.getForm(this._formElementId);

        if(this._targetControlId!=null) {
            var targetControl=$get(this._targetControlId);
            if(targetControl!=null) {
                if(this._targetControlDelegate===null) {
                    this._targetControlDelegate=Function.createDelegate(this,this._targetControlHandler);
                }
                Sys.UI.DomEvent.addHandler(targetControl,'click',this._targetControlDelegate);
            }
        }
        if(this._mouseDownDelegate===null)
            this._mouseDownDelegate=Function.createDelegate(this,this._mouseDownHandler);

        this._createWindow();
        this._setLocation();
        this._createBlockScreen();
        if(this._okButtonId!=null&&this._okButtonId!='') {
            var ok=$get(this._okButtonId);
            if(this._okButtonDelegate===null)
                this._okButtonDelegate=Function.createDelegate(this,this._okButtonHandler);

            if(ok!==null) {
                Sys.UI.DomEvent.addHandler(ok,'click',this._okButtonDelegate);
                Sys.UI.DomEvent.addHandler(ok,'mousedown',this._mouseDownDelegate);
            }
        }
        if(this._cancelButtonId!=null&&this._cancelButtonId!='') {
            var cancel=$get(this._cancelButtonId);
            if(this._cancelButtonDelegate===null)
                this._cancelButtonDelegate=Function.createDelegate(this,this._cancelButtonHandler);
            if(cancel!==null) {
                Sys.UI.DomEvent.addHandler(cancel,'click',this._cancelButtonDelegate);
                Sys.UI.DomEvent.addHandler(cancel,'mousedown',this._mouseDownDelegate);
            }
        }

        this.show(this._display);

        this._onResizeHandlerDelegate=Function.createDelegate(this,this._onResizeHandler);
        Sys.UI.DomEvent.addHandler(window,'resize',this._onResizeHandlerDelegate);

        this._onScrollHandlerDelegate=Function.createDelegate(this,this._onScrollHandler);
        Sys.UI.DomEvent.addHandler(window,'scroll',this._onScrollHandlerDelegate);

    },
    _onResizeHandler: function (event) {
        this._setLocation();
        this._applyBlockScreenBounds();
    },
    _onScrollHandler: function (event) {
        this._setLocation();
        this._applyBlockScreenBounds();
    },
    resetTitle: function (val) {
        var caption=$get(this._id+'_caption');
        if(caption!=null)
            caption.innerHTML=val;
    },
    _mouseDownHandler: function (args) {
        args.preventDefault();
    },
    _targetControlHandler: function (args) {
        this.show(true);
        var h=this.get_events().getHandler('target');
        if(h) h(this,args);
    },
    _okButtonHandler: function (args) {
        this.show(false);
        var h=this.get_events().getHandler('ok');
        if(h) h(this,args);
    },
    _cancelButtonHandler: function (args) {
        this.show(false);
        var h=this.get_events().getHandler('cancel');
        if(h) h(this,args);
    },
    _closeHandler: function (args) {
        this.show(false);
        var h=this.get_events().getHandler('close');
        if(h) h(this,args);
    },
    show: function (val) {
        this._toggleSelectElement();
        if(val) {
            this._popup.style.zIndex=Typps.Utility.incrementZIndex(2);
            this._popup.style.display='';
        }
        else {
            this._popup.style.display='none';
        }
        this._toggleBlockScreen();
        if(val) {
            this._setLocation();
            this._applyBlockScreenBounds();
        }
    },
    _toggleSelectElement: function () {
        this._selects=Typps.Utility.toggleSelectElement(this._popup,this._selects);
    },
    _createBlockScreen: function () {
        if(!this._isModal)
            return;
        var id=this.get_id()+'_dialogframe';
        var shadeElement=Typps.Utility.createBlockScreen(id,this._shaderColor,this._shaderOpacity);
    },
    _toggleBlockScreen: function () {
        var zIndex=isNaN(parseInt(this._popup.style.zIndex))?0:parseInt(this._popup.style.zIndex)-1;
        var shadeElement=$get(this.get_id()+'_dialogframe');
        Typps.Utility.toggleBlockScreen(shadeElement,zIndex);
    },
    _applyBlockScreenBounds: function () {
        var id=this.get_id()+'_dialogframe';
        Typps.Utility.applyBlockScreenBounds(id);
    },
    _setLocation: function () {
        var elem=$get(this._id+'_popup');
        Typps.Utility.setLocation(elem);
    },
    _createWindow: function () {
        this._popup=document.createElement('table');
        var oTBody=document.createElement('tbody');
        this._popupHeader=document.createElement('tr');

        var caption=document.createElement('td');
        var buttonCell=document.createElement('td');

        var button=document.createElement('a');
        button.href='javascript:void(0);';

        var oRow2=document.createElement('tr');
        var bodyCell=document.createElement('td');
        bodyCell.colSpan=2;

        this._popup.appendChild(oTBody);
        oTBody.appendChild(this._popupHeader);
        oTBody.appendChild(oRow2);

        buttonCell.appendChild(button);
        this._popupHeader.appendChild(caption);
        this._popupHeader.appendChild(buttonCell);

        bodyCell.appendChild(this._content);
        oRow2.appendChild(bodyCell);

        if(this._makeFormChild)
            this._form.appendChild(this._popup);
        else
            document.body.appendChild(this._popup);

        var width=(this._content.offsetWidth+5);
        var height=(this._content.offsetHeight+5);

        this._popup.id=this._id+'_popup';
        this._popupHeader.id=this._id+'_popupHeader';
        oRow2.id=this._id+'_popupBody';
        caption.id=this._id+'_caption';
        button.id=this._id+'_popupCloseButton';
        if (!Typps.Utility.isNullOrEmpty(this._className)) {
            this._addCssClass(this._popup,this._className+'-popup',Typps.Perform.Add);
            this._addCssClass(this._popupHeader,this._className+'-popupheader',Typps.Perform.Add);
            this._addCssClass(oRow2,this._className+'-popupbody',Typps.Perform.Add);
            this._addCssClass(caption,this._className+'-popupheadercaption',Typps.Perform.Add);
            this._addCssClass(button,'tristateclosebutton16x48',Typps.Perform.Add);
        }
        caption.innerHTML=this._title;
        if(Sys.Browser.agent==Sys.Browser.InternetExplorer&&Sys.Browser.version==6)
            this._popupHeader.style.position='relative';

        this._popup.style.position='absolute';
        this._popup.cellSpacing='0';
        this._popup.cellPadding='0';
        this._popup.style.width=width+'px';
        this._popup.style.height=height+'px';
        // center the element on screen 

        this._content.style.marginLeft='auto';
        this._content.style.marginRight='auto';
        caption.style.cursor='move';
        button.style.cursor='pointer';
        buttonCell.style.textAlign='right';
        var buttonWidth=button.offsetWidth;
        var buttonCellWidth=buttonCell.offsetWidth;
        buttonCell.style.width=buttonWidth+'px';
        //ie6 makes the entire bar draggable only if i set
        //a background color on caption or if the caption is long enough
        //otherwise only the parts in the cation that contain the title are draggable.
        //too bad :-)
        if(this._startDragDelegate===null) {
            this._startDragDelegate=Function.createDelegate(this,this._startDragHandler);
        }
        if(this._closeDelegate===null) {
            this._closeDelegate=Function.createDelegate(this,this._closeHandler);
        }
        Sys.UI.DomEvent.addHandler(button,'click',this._closeDelegate);
        Sys.UI.DomEvent.addHandler(button,'mousedown',this._mouseDownDelegate);
        Sys.UI.DomEvent.addHandler(caption,'mousedown',this._startDragDelegate);
    },
    resetCaption: function (val) {
        var caption=$get(this._id+'_caption');
        if(caption!=null)
            caption.innerHTML=val;
    },
    _startDragHandler: function (event) {

        if(Sys.Browser.agent==Sys.Browser.InternetExplorer)
            document.onselectstart=new Function("return false");

        Typps.Utility.setTransparency(this._popup,50);

        if(event.clientX||event.clientY) {
            this._cursorX=event.clientX-parseInt(this._popup.style.left)+5;
            this._cursorY=event.clientY-parseInt(this._popup.style.top)+5;
        }
        if(this._moveDelegate===null) {
            this._moveDelegate=Function.createDelegate(this,this._moveHandler);
            Sys.UI.DomEvent.addHandler(document,'mousemove',this._moveDelegate);
        }
        if(this._stopDragDelegate===null) {
            this._stopDragDelegate=Function.createDelegate(this,this._stopDragHandler);
            Sys.UI.DomEvent.addHandler(document,'mouseup',this._stopDragDelegate);
        }
        event.preventDefault();
        return false
    },
    _moveHandler: function (event) {
        var x=event.clientX-this._cursorX;
        var y=event.clientY-this._cursorY;

        this._popup.style.left=x+'px';
        this._popup.style.top=y+'px';
        this._popupHeader.focus();

    },
    _stopDragHandler: function (event) {
        if(this._moveDelegate) {
            if(this._popup!=null)
                Sys.UI.DomEvent.removeHandler(document,'mousemove',this._moveDelegate);
            delete this._moveDelegate;
            this._moveDelegate=null;
        }
        if(this._stopDragDelegate) {
            if(this._popup!=null)
                Sys.UI.DomEvent.removeHandler(document,'mouseup',this._stopDragDelegate);
            delete this._stopDragDelegate;
            this._stopDragDelegate=null;
        }
        Typps.Utility.setTransparency(this._popup,100);
        this._moveHandler(event);
    },
    _toggleCssClass: function (elem,className) {
        Typps.Utility.addRemoveClassName(elem,className,Typps.Perform.Toggle);
    },
    _addCssClass: function (elem,className) {
        Typps.Utility.addRemoveClassName(elem,className,Typps.Perform.Add);
    },
    _removeCssClass: function (elem,className) {
        Typps.Utility.addRemoveClassName(elem,className,Typps.Perform.Remove);
    }
}
Typps.ModalPopup.inheritsFrom(Sys.Component);
Typps.ModalPopup.registerClass('Typps.ModalPopup',Sys.Component);
// Since this script is not loaded by System.Web.Handlers.ScriptResourceHandler
// invoke Sys.Application.notifyScriptLoaded to notify ScriptManager 
// that this is the end of the script.
if(typeof (Sys)!=='undefined') Sys.Application.notifyScriptLoaded();